package com.example.service.admin;

import com.example.entity.Admin;
import com.example.entity.DTO.AdminDetailDTO;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Resource
    private AdminMapper adminMapper;

    public List<AdminDetailDTO> selectAll() {
        return adminMapper.selectAll();
    }

    public void add(Admin admin) {
        String username = admin.getUsername();
        Admin dbAdmin = adminMapper.selectByUsername(username);
        if(dbAdmin != null) {
            throw new CustomException("500", "用户名已存在!");
        }
        admin.setRole("ADMIN");
        adminMapper.insert(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    public void update(Admin admin) {
        adminMapper.updateById(admin);
    }
}
